<?php
/*
  $Id: cc.php,v 1.1.1.1 2004/03/04 23:41:17 ccwjr Exp $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/

  class po {
    var $code, $title, $description, $enabled;

// class constructor
    function po() {
      global $order;

      $this->code = 'po';
      $this->title = MODULE_PAYMENT_PO_TEXT_TITLE;
      $this->description = MODULE_PAYMENT_PO_TEXT_DESCRIPTION;
	  $this->email_footer = MODULE_PAYMENT_PO_TEXT_EMAIL_FOOTER;
      $this->sort_order = MODULE_PAYMENT_PO_SORT_ORDER;
      $this->enabled = ((MODULE_PAYMENT_PO_STATUS == 'True') ? true : false);
    }

// class methods

	function selection() {
      global $order;

      $selection = array('id' => $this->code,
                         'module' => $this->title,
                         'fields' => array(array('title' => MODULE_PAYMENT_PO_TEXT_NUMBER,
                                                 'field' => tep_draw_input_field('po_number', ''))));

      return $selection;
    }


    function javascript_validation() {
     /* $js = '  if (payment_value == "' . $this->code . '") {' . "\n" .
            '    var cc_owner = document.checkout_payment.cc_owner.value;' . "\n" .
            '    var cc_number = document.checkout_payment.cc_number.value;' . "\n" .
            '    if (cc_owner == "" || cc_owner.length < ' . CC_OWNER_MIN_LENGTH . ') {' . "\n" .
            '      error_message = error_message + "' . MODULE_PAYMENT_CC_TEXT_JS_CC_OWNER . '";' . "\n" .
            '      error = 1;' . "\n" .
            '    }' . "\n" .
            '    if (cc_number == "" || cc_number.length < ' . CC_NUMBER_MIN_LENGTH . ') {' . "\n" .
            '      error_message = error_message + "' . MODULE_PAYMENT_CC_TEXT_JS_CC_NUMBER . '";' . "\n" .
            '      error = 1;' . "\n" .
            '    }' . "\n" .
            '  }' . "\n";
	*/
      return $js;
    }

    function pre_confirmation_check() 
	{
		global $_POST;
		global $order;
		global $customer_id;


		$error = '';
		
		$check_credit = tep_db_query("SELECT customers_credit_account_status,customers_credit_status, customers_credit_left from " . TABLE_CUSTOMERS . " where customers_id ='" . $customer_id . "'");
	  
		$credit = tep_db_fetch_array($check_credit);
		
		If ($credit['customers_credit_account_status'] =='1' )
		{
			If ($_POST['po_number'] == '')
			{
				$error = sprintf(MODULE_PAYMENT_PO_TEXT_ERROR_NO_NUMBER);
			}
			else
			{
				if ($credit['customers_credit_status'] == "1")
				{
					if ($order->info['total'] > $credit['customers_credit_left'])
					{
						$error = sprintf(MODULE_PAYMENT_PO_TEXT_ERROR_NOT_ENOUGH_CREDIT. $credit['customers_credit_left']);
					}
				}
				else 
				{
					$error = MODULE_PAYMENT_PO_TEXT_ERROR_CREDIT_DISABLED;
				}
			}
		}
		else
		{
		
			$error = sprintf(MODULE_PAYMENT_PO_TEXT_ERROR_NO_CREDIT_ACCOUNT);
		}
	  If ($error)
	  {
		   $payment_error_return = 'payment_error=' . $this->code . '&error=' . urlencode($error) ;
		  tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false));
		}
    }

    function confirmation() {
      global $_POST;

      $confirmation = array('title' => $this->title,
                            'fields' => array(array('title' => MODULE_PAYMENT_PO_TEXT_NUMBER,
                                                    'field' => $_POST['po_number'])));
      return $confirmation;
    }

    function process_button() {
      global $_POST;

      $process_button_string = tep_draw_hidden_field('po_number', $_POST['po_number']);
      return $process_button_string;
    }

    function before_process() {
      global $_POST, $order;
	
	$order ->info['po_number'] = $_POST['po_number'];
    }

    function after_process() 
	{
		return false;
    }

    function get_error() {
      global $HTTP_GET_VARS;

      $error = array('title' => MODULE_PAYMENT_PO_TEXT_ERROR,
                     'error' => stripslashes(urldecode($HTTP_GET_VARS['error'])));

      return $error;
    }

    function check() {
      if (!isset($this->check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_PO_STATUS'");
        $this->check = tep_db_num_rows($check_query);
      }
      return $this->check;
    }

    function install() {
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable credit account Module', 'MODULE_PAYMENT_PO_STATUS', 'True', 'Do you want to accept payments by credit account?', '6', '0', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort order of display.', 'MODULE_PAYMENT_PO_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '0' , now())");
    }

    function remove() {
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
      return array('MODULE_PAYMENT_PO_STATUS', 'MODULE_PAYMENT_PO_SORT_ORDER');
    }
  }
?>
